<template>
	<view class="container">
		<view class="xu"    :style="{background: unprogressColor,width:width,height:height}">
			<view class="shi" :style="{
					width:currentProgress + '%' ,
					backgroundColor:bodyColor,
					borderBlockColor:boderColor
				}">
				<view class="xg" :style="{background: boderColor}" v-for="item in column"></view>
				
			</view>
			<view class="btn" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd" :style="{background: bodyColor,
				borderColor:boderColor,color:textColor}">{{currentProgress}}%</view>
		</view>

	</view>
</template>

<script>
	export default {
		name: "colorfulprogress",
		props: {
			//进度值
			progress: {
				type: Number,
				default: 0,
				validator: value => value >= 0 && value <= 100
			},

			//进度颜色
			bodyColor: {
				type: String,
				default: '#00B5FF'
			},
			//控件宽度
			width: {
				type: String,
				default: '264rpx'
			},
			//控件高度
			height: {
				type: String,
				default: '24rpx'
			},//滑块和纹路颜色
			boderColor: {
				type: String,
				default: '#0084F0'
			},
			textColor:{
				type: String,
				default: '#ffffff'
			},
			//无进度背景色
			unprogressColor: {
				type: String,
				default: '#F1F1F1'
			},
			//是否只读
			readonly: {
				type: Boolean,
				default: false
			}
		},

		watch: {
			progress(newVal) {
				this.currentProgress = newVal;
			}
		},
		mounted() {
			// 使用 uni.createSelectorQuery 获取 progressBar 的宽度和起始位置
			this.getProgressBarInfo();
		},
		data() {
			return {
				currentProgress: this.progress,
				progressBarWidth: 0,
				progressBarLeft: 0,
				column:0
			};
		},
		methods: {
			getProgressBarInfo() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.xu').boundingClientRect(data => {
					if (data) {
						this.progressBarWidth = data.width;
						this.progressBarLeft = data.left;
						this.column=Math.round(this.progressBarWidth/ (5 + 2.5) * this.progress/100)
					}
				}).exec();
				
			},
			onTouchStart(event) {
				this.updateProgress(event.touches[0].pageX);
			},
			onTouchMove(event) {
				
				this.updateProgress(event.touches[0].pageX);
			},
			onTouchEnd() {
				
				this.$emit('update:progress', this.currentProgress);
				//this.$emit('value',this.currentProgress)
			},
			updateProgress(x) {
				if(!this.readonly){
					// 计算滑动的位置对应的进度百分比
					const newProgress = Math.min(100, Math.max(0, ((x - this.progressBarLeft) / this.progressBarWidth) * 100));
					this.currentProgress = Math.round(newProgress);
					this.column=Math.round(this.progressBarWidth/ 7 * this.currentProgress/100)
				}
				
			}
		}

	}
</script>

<style lang="scss">
	.container {
		position: relative;
	}

	.xu {
		// width: 264rpx;
		// height: 24rpx;
		border-radius: 15rpx;
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}
.btn {
			width: 58rpx;
			height: 32rpx;
			font-family: Helvetica, Helvetica;
			font-weight: bold;
			font-size: 22rpx;
			line-height: 26rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-style: normal;
			margin-left: -15rpx;
			//padding: 20rpx;
			border-radius: 16rpx;
			border: 2rpx solid ;
			z-index: 1;
		}
	.shi {
		height: 100%;
		border-radius: 15rpx;
		overflow: hidden;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		.xg{
			height: 90%;
			width: 5rpx;
			margin-left: 10rpx;
			 transform: rotate(10deg); /* 旋转 10 度 */
			 transform-origin: center; /* 旋转中心，默认即为中心 */
		}

		

	}
</style>